package cn.lan.scau_hotel_service.controller;

import cn.lan.scau_hotel_service.entity.*;
import cn.lan.scau_hotel_service.service.*;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;

    @Autowired
    private RoomService roomService;

    @Autowired
    private HotelService hotelService;

    @Autowired
    private RoomTypeService roomTypeService;

    @RequestMapping(value = "/updateStatus/{order_id}/{status}",method= RequestMethod.GET)
    public String updateOrderStatus(@PathVariable int order_id,@PathVariable int status){
        Order order = orderService.getOrderById(order_id);
        order.setStatus(status);
        orderService.saveOrder(order);
        return "更改成功";
    }

    @ApiOperation(value="添加和更新订单", notes="需要的参数一定要有 user_id,而不是 openid")
    @ApiImplicitParam(name = "serverEntity", value = "包含order和其他", required = true, dataType = "ServerEntity")
    @RequestMapping(value="/{user_id}/{hotel_id}", method= RequestMethod.POST)
    public String addAndUpdateOrder(@RequestBody ServerEntity serverEntity, @PathVariable int user_id, @PathVariable int hotel_id) {
        User user = userService.getUserById(user_id);
        Hotel hotel = hotelService.getHotelById(hotel_id);
        Order order = serverEntity.getOrder();
        int roomNumber = serverEntity.getRoomNumber();
        int roomType_id = serverEntity.getRoomType_id();
        order.setHotel(hotel);
        order.setBooker(user);
        List<Room> rooms = roomService.getRoomsByRoomTypeAndCanRentTrueAndIsRentFalse(roomTypeService.getRoomTypeById(roomType_id));
        System.out.println("++++++++++++++++++++++++++++++++++++++=: " + rooms.size());
        for(int i=0;i<roomNumber ;i++){
            rooms.get(i).setRent(true);
        }
        List<Room> roomList = new ArrayList<>();
        for(int i=0; i<roomNumber;i++){
            roomList.add(rooms.get(i));
        }
        order.setRoomList(roomList);
        System.out.println("------------------------------------------------------------");
        System.out.println(order.toString());
        System.out.println("user_id: " + user_id);
        return  "保存和更新结果：" + orderService.saveOrder(order);
    }

    @ApiOperation(value="获取所有订单列表", notes="不需要参数")
    @RequestMapping(value="/list", method= RequestMethod.POST)
    public List<Order> getOrders() {
        return orderService.findAll();
    }

    @ApiOperation(value = "获取订单列表",notes = "根据用户的id,获取用户订单列表")
    @ApiImplicitParam(name = "id", value = "用户实体id", required = true, dataType = "int")
    @RequestMapping(value="/{id}", method= RequestMethod.GET)
    public List<Order> getOrders(@PathVariable int id) {
        User user = userService.getUserById(id);
        return orderService.getOrderByBooker(user);
    }


}
